package org.shj.thread.book2.chapter06.improve;


public class Data extends AbstractData{
	
	public Data(int size){
		super(size);
	}
	
	protected char[] doRead(){
		//System.out.println("======== " + Thread.currentThread().getName() + " is reading...");
		char[] newbuf = new char[buffer.length];
		for(int i = 0 ; i < buffer.length; i++){
			newbuf[i] = buffer[i];
		}
		
		slowly();
		//System.out.println("======== " + Thread.currentThread().getName() + " finish reading...");
		return newbuf;
	}
	
	protected void doWrite(char c){
		System.out.println("======== " + Thread.currentThread().getName() + " is writing...");
		for(int i = 0 ; i < buffer.length; i++){
			buffer[i] = c;
			slowly(); //在循环里调用，模拟写的操作会比读的操作要花的时间长
		}
		System.out.println("======== " + Thread.currentThread().getName() + " finish writing...");
	}
	
	//模拟比较花费时间的操作
	private void slowly(){
		try{
			Thread.sleep(50);
		}catch(InterruptedException e){
			
		}
	}
}
